<?php
namespace Home\Controller;
/**
 * show girl
 * @author liubo <liubo@ilongyuan.com.cn>
 */
class ShowgirlController extends HomeController {

    public function _initialize() {
        parent::_initialize();
        C('DEFAULT_THEME', 'v4');
    }

    public function index()
    {
        $this->showgirls = event('Showgirl')->lists([], '', 1, 6);

        // $user_ids = array_column($this->showgirls, 'user_id');
        // $map = ['uid' => ['in', $user_ids]];
        $vids = C('SHOWGIRL_VIDEOS');
        $map = ['id' => ['in', array_map('intval', $vids)]];
        $this->videos = event('Video')->getBaseInfoLists($map, true, '', 1, 4);

        $this->title = 'ShowGirl';
        if (IS_MOBILE) {
            $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
            $isios = strpos($agent, 'iphone') || strpos($agent, 'ipad');
            $this->type = $isios ? 'ios' : 'android';

            C('DEFAULT_THEME', 'mobile4');
        }
        $this->display();
    }

    public function ajaxComments()
    {
        $list = [];
        $comment_config = C('SHOWGIRL_COMMENT');
        foreach ($comment_config as $value) {
            $v = explode('|', $value);
            $list[] = ['id' => intval($v[0]), 'content' => $v[1]];
        }

        $showgirl_ids = array_column($list, 'id');
        $map['id'] = ['in', $showgirl_ids];
        $showgirl_list = event('Showgirl')->lists($map);

        $showgirl_list = array_column($showgirl_list, null, 'id');
        foreach ($list as &$value) {
            $value['showgirl'] = $showgirl_list[$value['id']];
        }
        $this->ajaxReturn(['state' => 0, 'data' => $list]);
    }

    public function download()
    {
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $isios = strpos($agent, 'iphone') || strpos($agent, 'ipad');

        if ($isios) {
            $iosRes = event('AppVersion')->getLatest('ios');
            redirect($iosRes['url']);
        } else {
            redirect(C('SHOWGIRL_ANDROID_DOWNLOAD'));
        }
    }
}